package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddCateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_cate")
@Slf4j
public class YddCateController {

    @Autowired
    private IYddCateService iYddCateService;

    @Autowired
    private IYddArticleService iYddArticleService;


    @GetMapping("/list")
    public ResultData list(String catename , int page, int limit){
        log.info("查询分类");
        if (catename !=null){
            catename = catename.trim();
        }

        //分页查询控制器
        Page pageInfo =new Page(page,limit);

        //查询条件
        LambdaQueryWrapper<YddCate> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(catename),YddCate::getCatename,catename);
        lqw.orderByDesc(YddCate::getSortNum);

        //执行查询
         iYddCateService.page(pageInfo, lqw);


        long count = pageInfo.getTotal();
        long pages = pageInfo.getPages();
        long size = pageInfo.getSize();
        long current = pageInfo.getCurrent();
        List records = pageInfo.getRecords();
        Map<String,Object> map =new HashMap<>();
        map.put("count",count);
        map.put("totalPages",pages);
        map.put("pageSize",size);
        map.put("currentPage",current);
        map.put("data",records);

        return ResultData.ok("",map);
    }

    @PostMapping("add")
    public ResultData add(@RequestBody YddCate yddCate){
        if (getResultData(yddCate)) {
            return ResultData.error("分类已存在");
        }
        iYddCateService.save(yddCate);
        return ResultData.ok("","");
    }

    @GetMapping("del")
    public ResultData del(int id){
        LambdaQueryWrapper<YddArticle> lqw =new LambdaQueryWrapper<>();
        lqw.eq(YddArticle::getCateid,id);
        List<YddArticle> list = iYddArticleService.list(lqw);
        log.info("list集合{}",list);
        if (!CollectionUtils.isEmpty(list)){
            //集合有数据，不能删除
            return ResultData.error("有文章关联");
        }
        //集合为空，表示没有表关联，可以进行删除操作
        iYddCateService.removeById(id);
        return ResultData.ok("","");


    }

    @GetMapping("rowInfo")
    public ResultData rowInfo(int id){
        YddCate byId = iYddCateService.getById(id);
        return ResultData.ok("",byId);
    }


    @PostMapping("update")
    public ResultData update(@RequestBody YddCate yddCate){
        if (getResultData(yddCate)) {
            return ResultData.error("分类已存在");
        }
        iYddCateService.updateById(yddCate);
        return ResultData.ok("","");
    }

    @GetMapping("all")
    public ResultData all(){
        List<YddCate> list = iYddCateService.list();
        return ResultData.ok("",list);
    }

    //判断分类是否存在
    private boolean getResultData(YddCate yddCate) {
        List<YddCate> list = iYddCateService.list();
        for (YddCate cate : list) {
            if (cate.getCatename().equals(yddCate.getCatename())) {
                return true;
            }
        }
        return false;
    }

}
